package com.example.springboot.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Audit;
import com.example.springboot.entity.Review;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.ReviewMapper;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.AuditService;
import com.example.springboot.service.ReviewService;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@CrossOrigin
@RestController
@RequestMapping("/audit")
public class AuditController {
    @Autowired
    AuditService auditService;
    @Autowired
    UserService userService;
    @Resource
    UserMapper userMapper;
    @Resource
    ReviewService reviewService;

    //    新增用户信息
    @PostMapping("/add")
    public Result add(@RequestBody Audit audit){
        try {
//                User user = userMapper.selectById(audit.getUserId());
//                if (ObjectUtil.isNotEmpty(user)) {
//                    audit.setName(user.getName());
//                    audit.setUsername(user.getUsername());
//                    audit.setPhone(user.getPhone());
//                    audit.setEmail(user.getEmail());
//                    audit.setAvatar(user.getAvatar());
//                }
            auditService.save(audit);
            Integer auditId =audit.getId();
            Integer userId =audit.getUserId();
            Review review =new Review();
            review.setAuditId(auditId);
            review.setUserId(userId);
            reviewService.save(review);
        }catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据异常");
            }else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }
    //修改用户信息
    @PutMapping("/update")
    public Result update(@RequestBody Audit audit){
        auditService.updateById(audit);
        return Result.success();
    }
    //删除用户
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        auditService.removeById(id);
        return Result.success();
    }
    //查询全部用户信息
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Audit> auditList = auditService.list(new QueryWrapper<Audit>().orderByDesc("id"));  //select * from user order by id desc
        return Result.success(auditList);
    }

    //根据ID查询用户信息
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Audit audit= auditService.getById(id);
        return Result.success(audit);
    }

    //多条件分页模糊查询用户信息

    @GetMapping("/selectByPage")
    public Result selectByPage( @RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam String username,
                                @RequestParam String projectName
                                ){
        QueryWrapper<Audit> queryWrapper = new QueryWrapper<Audit>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(username), "username" ,username);
        queryWrapper.like(StrUtil.isNotBlank(projectName), "project_name" ,projectName);
        // select *from user where username like '%#{username}%' and name like '%#{name}%'
        User user =TokenUtils.getCurrentUser();
        if (ObjectUtil.isNull(user)){
            return Result.error("请登录");
        }
        if ("学生".equals(user.getRole())){
            queryWrapper.eq("user_Id",user.getId());
        }
        Page<Audit> page = auditService.page(new Page<>(pageNum, pageSize), queryWrapper);
        auditService.getAuditWithUser(page,queryWrapper);
        return Result.success(page);
    }
}